package logic

import (
	"context"
	"user-login/moder/mysql"

	"user-login/userrpc/internal/svc"
	"user-login/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserListLogic) UserList(in *userrpc.UserListRequest) (*userrpc.UserListResponse, error) {
	// todo: add your logic here and delete this line

	var user []mysql.User
	mysql.DB.Scopes(mysql.Paginate(int(in.Page), int(in.Size))).Find(&user)
	var userList []*userrpc.UserList
	for _, users := range user {
		list := userrpc.UserList{
			Username: users.Username,
			Password: users.Password,
			UserId:   int64(users.ID),
		}
		userList = append(userList, &list)
	}
	return &userrpc.UserListResponse{
		List: userList,
	}, nil
}
